const mutations = {
  /* token */
  SETTOKEN(state, token) {
    state.Authorization = token;
    localStorage.setItem('Authorization', token);
  },
  /* 用户名 */
  LOGIN(state, obj) {
    state.userName = obj.userName;
    localStorage.setItem('userName', obj.userName);
  },
  /* 角色信息 */
  SETROLELIST(state, list) {
    state.roleList = list;
  },
  /* 导航菜单 */
  SETMENULIST(state, list) {
    state.menuList = list;
  },
  /* 新增标签页 */
  ADDTAB(state, obj) {
    /* 已存在则不新增 */
    for (let i = 0; i < state.editableTabs.length; i++) {
      if (state.editableTabs[i].name === obj.name) {
        state.editableTabsValue = obj.name;
        return
      }
    }

    const idx = obj.index[1].split("-");
    const data = state.menuList[idx[0]].list[idx[1]];// 激活的导航菜单信息

    state.editableTabs.push({
      title: data.name,
      name: obj.name,
      content: data.url
    });
    state.editableTabsValue = obj.name;
  },
  /* 删除标签页 */
  REMOVETAB(state, val) {
    let tabs = state.editableTabs;
    let activeName = state.editableTabsValue;
    if (activeName === val) {
      tabs.forEach((tab, index) => {
        if (tab.name === val) {
          let nextTab = tabs[index + 1] || tabs[index - 1];
          if (nextTab) {
            activeName = nextTab.name;
          }
        }
      });
    }
    state.editableTabsValue = activeName;
    state.editableTabs = tabs.filter(tab => tab.name !== val);
  }
};
export default mutations;